package com.lzb.browser.activity;

import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;

import com.lzb.browser.BaseAc;
import com.lzb.browser.Content;
import com.lzb.browser.HomeActivity;
import com.lzb.browser.R;
import com.lzb.browser.basic.utils.SPUtil;
import com.lzb.browser.databinding.ActivityCompassBinding;

public class CompassActivity extends BaseAc<ActivityCompassBinding> {

    private Handler longPressHandler;
    private Runnable longPressRunnable;
    private boolean isLongPressing = false;

    @Override
    protected int onCreate() {
        return R.layout.activity_compass;
    }

    @Override
    protected void initView() {
        setupLongPressListeners();
    }

    @Override
    protected void initData() {

    }

    /**
     * 设置长按监听器
     */
    private void setupLongPressListeners() {
        longPressHandler = new Handler();
        
        // 为整个布局设置触摸监听器，通过坐标判断触摸位置
        mDataBinding.llContent.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                handleTouchEvent(event);
                return true;
            }
        });
    }

    /**
     * 处理触摸事件，根据坐标判断是否在顶部或底部区域
     */
    private void handleTouchEvent(MotionEvent event) {
        float y = event.getY();
        float x = event.getX();
        int height = mDataBinding.getRoot().getHeight();
        int width = mDataBinding.getRoot().getWidth();
        
        // 扩大顶部区域和底部区域范围，让长按更容易触发
        float topAreaTop = height * 0.05f;      // 顶部区域开始位置（从15%扩大到5%）
        float topAreaBottom = height * 0.40f;   // 顶部区域结束位置（从35%扩大到40%）
        float bottomAreaTop = height * 0.60f;   // 底部区域开始位置（从65%缩小到60%）
        float bottomAreaBottom = height * 1f; // 底部区域结束位置（从85%扩大到100%）
        
        // 扩大水平检测范围，减少边缘限制
        float centerLeft = width * 0.1f;        // 左侧边界（从20%扩大到10%）
        float centerRight = width * 0.9f;       // 右侧边界（从80%扩大到90%）
        
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                // 检查是否在有效区域内（顶部或底部，且在水平中心区域）
                boolean inHorizontalCenter = x >= centerLeft && x <= centerRight;
                
                if (inHorizontalCenter) {
                    if (y >= topAreaTop && y <= topAreaBottom) {  // 顶部区域
                        startLongPress();
                    } else if (y >= bottomAreaTop && y <= bottomAreaBottom) {  // 底部区域
                        startLongPress();
                    }
                }
                break;
                
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                cancelLongPress();
                break;
        }
    }


    /**
     * 开始长按检测
     */
    private void startLongPress() {
        isLongPressing = true;
        
        longPressRunnable = new Runnable() {
            @Override
            public void run() {
                if (isLongPressing) {
                    onLongPressDetected();
                }
            }
        };
        longPressHandler.postDelayed(longPressRunnable, 2000); // 2秒长按
    }

    /**
     * 取消长按检测
     */
    private void cancelLongPress() {
        isLongPressing = false;
        if (longPressRunnable != null) {
            longPressHandler.removeCallbacks(longPressRunnable);
        }
    }

    /**
     * 长按事件触发
     */
    private void onLongPressDetected() {
        boolean isHave = SPUtil.getBoolean(getApplicationContext(), Content.isHavePassword, false);
        if (isHave){
            startActivity(InputPatternPswActivity.class);
            finish();
        }else {
            startActivity(HomeActivity.class);
            finish();
        }
    }



    @Override
    protected void onDestroy() {
        super.onDestroy();
        cancelLongPress();
        if (longPressHandler != null) {
            longPressHandler.removeCallbacksAndMessages(null);
        }
    }
}